package com.ecloud.common.web.config;

import java.util.Locale;

import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

/**
 * 动态国际化配置
 *
 * @author LiuGangQiang Create in 2020/03/29
 */
@Configuration
@AutoConfigureBefore(WebMvcAutoConfiguration.class)
public class AutoI18nConfiguration {

    /**
     * 设置 {@link LocaleResolver} 它有基于Session|Cookie|Fixed|Header几种模式<br>
     * 这里默认通过请求头Accept-Language获取,如果你想自定义参数请配置 {@link LocaleChangeInterceptor}并设置参数名<br>
     * 然后再实现 {@link WebMvcConfigurer}添加拦截器拦截
     *
     * @author LiuGangQiang Create in 2020/03/29
     * @return {@link LocaleResolver}
     */
    @Bean
    public LocaleResolver localeResolver() {
        AcceptHeaderLocaleResolver lr = new AcceptHeaderLocaleResolver();
        lr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
        return lr;
    }
}
